require("lsqlite3")
require("curl")

local cache_db = sqlite3.open("cache.db")

local function get(url)
	local temp = {}
	local c = curl.easy_init()
	print("GET", url) io.flush()
	c:setopt(curl.OPT_URL, url)
	c:setopt(curl.OPT_USERAGENT, "Firefox/2.0")
	c:setopt(curl.OPT_WRITEFUNCTION, function (s, len) temp[#temp + 1] = s ; return len end)
	local err, info = c:perform()
	if err ~= 0 then
		print("Error : "..info)
		return
	else
		temp = table.concat(temp)
		if temp:len() > 0 then
			return temp
		end
	end
end

local get_and_cache
do
	local insert_stmt = cache_db:prepare("INSERT INTO cache VALUES (?, ?)")
	
	function get_and_cache(url)
		local content = get(url)
		if content then
			insert_stmt:bind_values(url, content)
			insert_stmt:step()
			insert_stmt:reset()
		end
		return content
	end
end

local US = false

ARMORY_BASE_URL = US and "http://armory.worldofwarcraft.com" or "http://armory.wow-europe.com"

local getiteminfo = function (id)
	return get_and_cache(ARMORY_BASE_URL.."/item-info.xml?i="..id)
end

local getitemtooltip = function (id)
	return get_and_cache(ARMORY_BASE_URL.."/item-tooltip.xml?i="..id)
end

local id_count, id_min, id_max = 0
local existing = {}
for url in cache_db:urows("SELECT url FROM cache") do
	local n = url:match("item%-tooltip%.xml%?i=(%d+)$")
	if n then
		n = tonumber(n)
		existing[n] = true
		if not id_min or id_min > n then
			id_min = n
		end
		if not id_max or id_max < n then
			id_max = n
		end
		id_count = id_count + 1
	end
end
print(string.format("Already in cache : %d items from %d to %d", id_count, id_min, id_max))

local start, _end = arg[1] or 1, arg[2] or 34000

local total = _end - start

local n, f, prev_percent = 0, 0, 0
for i = start, _end do
	if not existing[i] then
		local t = getitemtooltip(i)
		n = n + 1
		if t then
			getiteminfo(i)
			f = f + 1
		end
	end
	local percent = math.floor(i * 100 / total)
	if percent ~= prev_percent then
		print(percent, i, n, f)
		prev_percent = percent
	end
end

